var interface = require(path.join(__dirname, '_list.js')).interface;
var FormEX = require(path.join(__dirname, 'form.js'));
var selectEX = FormEX.select;

var search = function () {
    var Select = selectEX({
        showNameByDataKey: 'label',
        setValueByDataKey: 'value',
        getForm: 'value',
        "default": 'ALL',
        data: [{
            label: 'ALL',
            value: 'ALL'
        }, {
            label: 'GET',
            value: 'GET'
        }, {
            label: 'POST',
            value: 'POST'
        }, {
            label: 'PUT',
            value: 'PUT'
        }, {
            label: 'DEL',
            value: 'DEL'
        }],
        changeEvent: function (event) {
            event.get((error, doc) => {
                event.name.attr('class', 'form_select_name ' + doc);
            });
        }
    });

    Select.get((error, doc) => {
        Select.name.attr('class', 'form_select_name ' + doc);
    });


    this.html = $('<div id="search"></div>');
    this.inputEle = $('<div class="search_input"><div class="search_box"><div class="input_box"></div></div><i class="fa fa-search"></i></div>');
    this.inputBox = this.inputEle.find('.search_box');
    this.inputBox.append(Select.render());

    this.input = $('<input type="text"/>').keydown((e) => {
        if (e.keyCode == 13) {
            var text = this.input.val();
            if (text) {
                Select.get((error, doc) => {
                    this.event.renderList(this, text, doc);
                });
            };
        }
    });
    this.icon = this.inputEle.find('i').click(() => {
        var text = this.input.val();
        Select.get((error, doc) => {
            this.event.renderList(this, text, doc);
        });
    });

    this.inputEle.find('.input_box').append(this.input);
    this.list = $('<div class="search_list custom-scroll theme-scroll list"></div>');
    this.mark = null;
    // this.mark = $('<div class="mark"></div>');
    this.html.append(this.inputEle).append(this.list);
    this.event = {
        renderList: function () { },
        shallMenu: function () { },
        delete: function () { },
        move: function () { },
        active: function () { }
    };
    this.items = {};
    this.activeObj = null;
};

search.prototype = {
    constructor: this,
    render: function () {
        return this.html;
    },
    on: function (str, event) {
        if (this.event.hasOwnProperty(str)) {
            this.event[str] = event;
        };
    },
    setWidth: function (num) {
        this.html.css('width', num + 'px');
    },
    show: function () {
        this.html.css('display', 'block');
    },
    hide: function () {
        this.html.css('display', 'none');
    },
    showMark: function () {
        this.hideMark();
        this.mark = $('<div class="mark">未搜索到符合条件的结果</div>');
        this.list.append(this.mark);
    },
    hideMark: function () {
        if (this.mark) {
            this.mark.remove();
            this.mark = null;
        };
    },
    loading: function () {
        this.html.loadingEX();
    },
    each: function (cb) {
        for (const key in this.items) {
            if (this.items.hasOwnProperty(key)) {
                const element = this.items[key];
                cb(element);
            }
        }
    },
    unActive: function () {
        this.each((ele) => {
            ele.unActive();
        });
        this.activeObj = null;
    },
    active: function (id) {
        if (this.items.hasOwnProperty(id)) {
            if (this.items[id] !== this.activeObj) {
                this.unActive();
                this.items[id].active();
                this.activeObj = this.items[id];
                this.event.active(this.activeObj.config);
            };
        } else {
            this.unActive();
            this.activeObj = null;
        };
    },
    setData: function (_array) {
        this.clear();
        for (let i = 0; i < _array.length; i++) {
            const item = _array[i];
            this.items[item.id] = new interface(item, this);
            this.list.append(this.items[item.id].render());
        };
    },
    clear: function () {
        this.list.empty();
        this.items = {};
        this.activeObj = null;
        this.hideMark();
    },
};
module.exports = function () {
    return new search();
};